// pages/master/mainten/orderList/orderList.js
var app = getApp()
var util = require('../../../../utils/util.js')
var achieveDB=wx.cloud.database().collection('staffAchievement')
var utfDB=wx.cloud.database().collection('UsertoFamily')
//staffAchievement
Page({

  /**
   * 页面的初始数据
   */
  data: {
      userid:'',
      sideList:[],
      medicList:[],
      tuinaList:[],
      haveother:true,
      famnum:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.userID)
    let that = this
    that.setData({
      userid:options.userID
    })
    wx.showLoading({
      title: '正在查询...',
    })
    wx.cloud.callFunction({
      name: 'getUserOrder',// 要调用的云函数名称
      data: {             // 传递给云函数的event参数
        userid:options.userID
      }
    }).then(res => {
      console.log(res)
      that.setData({
        sideList:res.result.Sidedata.list,
        medicList:res.result.Meddata.list,
        tuinaList:res.result.Tuinadata.list
      })
      if(res.result.Sidedata.list.length>0||res.result.Meddata.list.length>0){
        that.setData({
          haveother:false
        })
      }
      wx.hideLoading()
    })
    utfDB.where({
      userID:options.userID
    }).get().then(res=>{
        that.setData({
          famnum:res.data[0].FamilyNum
        })
        // console.log(res.data[0].FamilyNum,that.data.famnum)
    })
  },
  clickMainten:function(res){
    let that = this
    //需要添加推拿师的业绩表
    // 推拿师id 商品id 服务用户id 维护时间 产品单价 商品数量 商品名称 
    wx.showLoading({
      title: '正在维护...',
    })
    var date = new Date()
    var _date = util.formatTime(date)//时间

    let achieveData = []
    let _tuina = that.data.tuinaList
    let _side = that.data.sideList
    let _medic = that.data.medicList
    let _tid  = app.userDate.userInfo.userPhone
    console.log(_tuina,_side,_medic)

    for (var i in _side){
      var obj = new Object()
      obj.teachID = _tid
      obj.goodsID = _side[i].OrderID
      obj.customerID = _side[i].UserID
      obj.date = _date
      obj.unitPrice = _side[i].TransactionAmount
      obj.salenum =  _side[i].OrderNum
      obj.goodsName = _side[i].SidelineName
      achieveData.push(obj)
  }
  for (var i in _tuina){
    var obj = new Object()
    obj.teachID = _tid
    obj.goodsID = _tuina[i].OrderID
    obj.customerID = _tuina[i].UserID
    obj.date = _date
    obj.unitPrice = _tuina[i].TransactionAmount
    obj.salenum =  _tuina[i].OrderNum
    obj.goodsName = _tuina[i].TuinaName
    achieveData.push(obj)
}

    for (var i in _medic){
        var obj = new Object()
        obj.teachID = _tid
        obj.goodsID = _medic[i].OrderID
        obj.customerID = _medic[i].UserID
        obj.date = _date
        obj.unitPrice = _medic[i].TransactionAmount
        obj.salenum =  _medic[i].OrderNum
        obj.goodsName = _medic[i].MedicName
        achieveData.push(obj)
    }
    console.log(achieveData)
  //   for (var i in achieveData){
  //   achieveDB.add({
  //     data:achieveData[i]
  //   })
  //   .then(res => {
  //     console.log(res)
  //   })
  // }
  let promiseMethod = new Array(achieveData.length)
  achieveData.forEach((item,i)=>{
    achieveDB.add({
      data:achieveData[i]
    })
    .then(res => {
      console.log(res)
    })
  })

  Promise.all([...promiseMethod]).then(() => {
    wx.cloud.callFunction({
      name: 'updateOrderData',// 要调用的云函数名称
      data: {             // 传递给云函数的event参数
        userid: this.data.userid
      }
    }).then(res => {
      console.log(res.result)
      if(res.result.stats.updated>0){
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '维护成功',
              icon:'success'
            })
            let pages = getCurrentPages()
            let prevPage = pages[pages.length - 2]
            // console.log(prevPage)
            prevPage.onLoad()
            wx.switchTab({
              url: '/pages/master/mainten/mainten',
            })
          },
        })
       
      }
    })
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})